package com.zprmb.udzt.fragment;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.zprmb.udzt.LoginActivity;
import com.zprmb.udzt.R;
import com.zprmb.udzt.adapter.Regular_Adapter1;
import com.zprmb.udzt.adapter.Regular_Adapter2;
import com.zprmb.udzt.adapter.Regular_Adapter3;
import com.zprmb.udzt.bean.RegularBean;
import com.zprmb.udzt.util.Constant;
import com.zprmb.udzt.util.ToJSON;
import com.zprmb.udzt.util.VolleryUtil;

public class Regular_fragment extends BaseFragment implements
		OnRefreshListener2<ListView> {
	private PullToRefreshListView lv_product_fragment;
	private ListView actualListView;
	private String title, token;
	private List<RegularBean> list = new ArrayList<RegularBean>();
	private List<RegularBean> list2 = new ArrayList<RegularBean>();
	private Regular_Adapter1 adapter;
	private Regular_Adapter2 adapter2;
	private Regular_Adapter3 adapter3;
	int page;
	private ImageView img_none;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		title = getArguments().getString("title");
		token = getArguments().getString("token");
		page = 1;

	}

	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.regular_fragment, container,
				false);
		lv_product_fragment = (PullToRefreshListView) view
				.findViewById(R.id.lv);
		img_none = (ImageView) view.findViewById(R.id.iv_v4_nonetwork);
		actualListView = lv_product_fragment.getRefreshableView();
		registerForContextMenu(actualListView);
		lv_product_fragment.setOnRefreshListener(this);
		lv_product_fragment.setMode(Mode.BOTH);
		initData();
		return view;
	}

	private void initData() {
		// TODO Auto-generated method stub

		VolleryUtil volleryUtil = new VolleryUtil() {

			@Override
			public void onUpDate(JSONObject jsonObject) {
				// TODO Auto-generated method stub
				Log.e("ss", jsonObject.toString());
				ToJSON.initData2(jsonObject.toString(), page + "");
				if (jsonObject.optInt("success") == 1) {
					Gson gson = new Gson();
					// if(title.equals("投标中")){
					// if (!jsonObject.optString("resultList").equals("null")) {
					// list=gson.fromJson(jsonObject.optString("resultList"),
					// new TypeToken<ArrayList<RegularBean>>() {
					// }.getType());
					//
					// }
					// list2.addAll(list);
					// adapter=new Regular_Adapter1(list2, getActivity());
					// lv_product_fragment.setAdapter(adapter);
					// adapter.notifyDataSetChanged();
					// actualListView.setSelection(list2.size()-10);
					// if(list2.size()<1){
					// lv_product_fragment.setVisibility(View.GONE);
					// img_none.setVisibility(View.VISIBLE);
					// }else{
					// lv_product_fragment.setVisibility(View.VISIBLE);
					// img_none.setVisibility(View.GONE);
					// }
					// }else
					if (title.equals("理财中")) {
						if (!jsonObject.optString("resultList").equals("null")) {
							list = gson.fromJson(
									jsonObject.optString("resultList"),
									new TypeToken<ArrayList<RegularBean>>() {
									}.getType());

						}
						list2.addAll(list);
						Log.e("ss", list2.toString());
						adapter2 = new Regular_Adapter2(list2, getActivity(),
								title);
						lv_product_fragment.setAdapter(adapter2);
						actualListView.setSelection(list2.size() - 5);
						adapter2.notifyDataSetChanged();
						if (list2.size() < 1) {
							lv_product_fragment.setVisibility(View.GONE);
							img_none.setVisibility(View.VISIBLE);
						} else {
							lv_product_fragment.setVisibility(View.VISIBLE);
							img_none.setVisibility(View.GONE);
						}
					} else if (title.equals("已回款")) {
						if (!jsonObject.optString("resultList").equals("null")) {
							list = gson.fromJson(
									jsonObject.optString("resultList"),
									new TypeToken<ArrayList<RegularBean>>() {
									}.getType());

						}
						list2.addAll(list);
						adapter2 = new Regular_Adapter2(list2, getActivity(),
								title);
						lv_product_fragment.setAdapter(adapter2);
						adapter2.notifyDataSetChanged();
						actualListView.setSelection(list2.size() - 5);
						if (list2.size() < 1) {
							lv_product_fragment.setVisibility(View.GONE);
							img_none.setVisibility(View.VISIBLE);
						} else {
							lv_product_fragment.setVisibility(View.VISIBLE);
							img_none.setVisibility(View.GONE);
						}
					}

				} else {
					if (jsonObject.optString("comment").equals("please login")) {
						Intent intent = new Intent(getActivity(),
								LoginActivity.class);
						startActivity(intent);
					} else {
						Toast.makeText(getActivity(),
								jsonObject.optString("comment"), 2000).show();
					}
				}

			}

			@Override
			public void onError() {
				// TODO Auto-generated method stub

			}
		};
		volleryUtil.setHeaders("token", token);
		volleryUtil.setRequestMethod(0);
		volleryUtil.setShow(true);
		// if(title.equals("投标中")){
		// volleryUtil.execute(getActivity(),
		// Constant.URL+"/api/v5/claim/investList?pageNo="+page+"&pageSize=10&status=FINISHED",
		// "");
		// }else
		if (title.equals("理财中")) {
			volleryUtil.execute(getActivity(), Constant.URL
					+ "/api/v5/claim/investList?pageNo=" + page
					+ "&pageSize=5&status=SETTLED", "");
		} else if (title.equals("已回款")) {
			volleryUtil.execute(getActivity(), Constant.URL
					+ "/api/v5/claim/investList?pageNo=" + page
					+ "&pageSize=5&status=CLEARED", "");
		}

	}

	public static Regular_fragment newInstance(int position, String string,
			String token) {
		Regular_fragment uf = new Regular_fragment();
		Bundle bundle = new Bundle();
		bundle.putString("token", token);
		bundle.putString("title", string);
		uf.setArguments(bundle);

		return uf;
	}

	Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			list.clear();
			if (msg.what == 1) {
				page = 1;
				list2.clear();
				initData();
			} else {

				page += 1;
				initData();

			}
			Toast.makeText(getActivity(), "刷新成功", 2000).show();
			lv_product_fragment.onRefreshComplete();
		};
	};

	@Override
	public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
		// TODO Auto-generated method stub
		handler.sendEmptyMessage(1);
	}

	@Override
	public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
		// TODO Auto-generated method stub
		handler.sendEmptyMessage(2);

	}
}
